#pragma once
#include "ReadCDB.h"
#include <iostream>
#include <fstream>
#include <chrono>

#include "AnsysAPDLReaderBase.h"
#include "ReadMesh.h"
#include "CDBReader.h"

bool ReadCDB(std::string exe_path, std::string& filePath, std::string output_path)
{
	try
	{
		using namespace std::chrono;
		auto _summary_time_start = system_clock::now();

		//bool importStatus = importCDBFile(filePath, if3d);

		//FDEBUG("Begin: import Ansys CDB File. %s", filePath.c_str());
		CDBReader* reader = CDBReader::Inst();
		CDBReader::AnalysisDimention3D = true;
		reader->init(filePath);
		if (!reader->Scan())
		{
			//FDEBUG("Failed: Scan file %s", reader->filePath().c_str());
		}
		reader->allocMem();
		if (reader->Read())
		{
			//FDEBUG("Read CDB file Succeed.");
		}
		else
		{
			//FDEBUG("Failed: Read CDB file failed.");
		}
		auto a = CDBReader::Inst()->write_vtk_file(exe_path, filePath, output_path);
		auto b = CDBReader::Inst()->write_gmsh_file_from_vtk(exe_path, filePath, output_path);

		//if (!importStatus)
		//{
		//	//FDEBUG("Failed: Read CDB file");
		//	return false;
		//}

		auto _summary_time_end = system_clock::now();
		auto duration = duration_cast<microseconds>(_summary_time_end - _summary_time_start);
		//FDEBUG("Time elapse: %lf sec", double(duration.count())* microseconds::period::num / microseconds::period::den);
#if 0

		if (ReadMesh::Inst()->GetNodeSetName().size() > 0)
		{
			ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Node"] = ReadMesh::Inst()->GetNodeSetName();
		}
		if (ReadMesh::Inst()->GetCellSetName().size() > 0)
		{
			ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Element"] = ReadMesh::Inst()->GetCellSetName();
		}
		if (ReadMesh::Inst()->GetSurfaceSetName().size() > 0)
		{
			ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Surface"] = ReadMesh::Inst()->GetSurfaceSetName();
		}
		if (ReadMesh::Inst()->GetNodeElementSurfaceSetName().size() > 1)
		{
			if (ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Node"].empty() || ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Element"].empty() || ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Surface"].empty())
			{
				// 如果有一个容器为空，则遍历剩下的两个容器
				if (!ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Node"].empty() && !ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Element"].empty()) {
					for (const std::string& value1 : ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Node"]) {
						for (const std::string& value2 : ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Element"]) {
							if (value1 == value2) {
								ReadMesh::Inst()->GetDuplicateSetName()["NodeElement"].push_back(value1);
							}
						}
					}
				}
				if (!ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Node"].empty() && !ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Surface"].empty()) {
					for (const std::string& value1 : ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Node"]) {
						for (const std::string& value3 : ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Surface"]) {
							if (value1 == value3) {
								ReadMesh::Inst()->GetDuplicateSetName()["NodeSurface"].push_back(value1);
							}
						}
					}
				}
				if (!ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Element"].empty() && !ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Surface"].empty()) {
					for (const std::string& value2 : ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Element"]) {
						for (const std::string& value3 : ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Surface"]) {
							if (value2 == value3) {
								ReadMesh::Inst()->GetDuplicateSetName()["ElementSurface"].push_back(value2);
							}
						}
					}
				}
			}
			else
			{
				for (const std::string& value1 : ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Node"]) {
					for (const std::string& value2 : ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Element"]) {
						if (value1 == value2) {
							ReadMesh::Inst()->GetDuplicateSetName()["NodeElement"].push_back(value1);
						}
					}
				}
				for (const std::string& value2 : ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Element"]) {
					for (const std::string& value3 : ReadMesh::Inst()->GetNodeElementSurfaceSetName()["Surface"]) {
						if (value2 == value3) {
							ReadMesh::Inst()->GetDuplicateSetName()["ElementSurface"].push_back(value2);
						}
					}
				}
			}
		}
#endif

		return true;
	}
	catch (...)
	{
		std::cerr << "Unknown error.\n";
		return false;
	}

}


